//
//  KZCheapVC.swift
//  BoyeStore
//
//  Created by 朱凯 on 2018/6/23.
//  Copyright © 2018年 itboye. All rights reserved.
//

import UIKit

class KZCheapVC: KZBaseViewController {

    var segmentBar = KZSegmentView()
    let sortView = KZSortView()
    let table = UITableView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        setNavigationBackgroungAttributes(naviTranslucent: true,
                                          naviBgColor: .clear)
        
        self.navigationController?.navigationBar.isHidden = true
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        self.navigationController?.navigationBar.isHidden = false
    }
    
    override func viewSafeAreaInsetsDidChange() {
        if #available(iOS 11.0, *) {
            table.snp.updateConstraints { (m) in
                m.bottom.equalTo(-view.safeAreaInsets.bottom)
            }
        }
    }
}

// MARK: ------ 创建视图
extension KZCheapVC {
    
    private func setupUI() {
        
        // 状态栏
        let statusBarBgView = UIView()
        statusBarBgView.backgroundColor = kThemeColor
        view.addSubview(statusBarBgView)
        statusBarBgView.snp.makeConstraints { (m) in
            m.left.top.right.equalTo(0)
            m.height.equalTo(UIApplication.shared.statusBarFrame.height)
        }
        
        // 搜索
        
        view.addSubview(segmentBar)
        segmentBar.config.backgroundColor = kThemeColor
        segmentBar.config.titleNormalColor = UIColor.white
        segmentBar.config.titleSelectColor = UIColor.white
        segmentBar.config.indicatorColor = UIColor.white
        segmentBar.config.indicatorAdaptTitleWidth = true
        segmentBar.config.indicatorHeight = 2
        segmentBar.config.indicatorBottomOffset = 8
        segmentBar.titles = ["一元购", "9.9包邮", "食品", "女装", "成人", "汽修"]
        segmentBar.snp.makeConstraints { (m) in
            m.left.right.equalTo(0)
            m.top.equalTo(statusBarBgView.snp.bottom).offset(0)
            m.height.equalTo(44)
        }
        
        // 排序
        view.addSubview(sortView)
        sortView.snp.makeConstraints { (m) in
            m.left.right.equalTo(0)
            m.height.equalTo(40)
            m.top.equalTo(segmentBar.snp.bottom)
        }
        sortView.sortClouse = { sortType, isPostive in
            
        }
        
        
        // 列表
        table.delegate = self
        table.dataSource = self
        table.separatorStyle = .none
        table.estimatedRowHeight = 200
        table.rowHeight = UITableViewAutomaticDimension
        table.backgroundColor = kBgColor
        table.register(KZSearchProductCell.self, forCellReuseIdentifier: "cell")
        view.addSubview(table)
        table.snp.makeConstraints { (m) in
            m.left.right.equalTo(0)
            m.top.equalTo(sortView.snp.bottom)
            m.bottom.equalTo(-49)
        }
        
        
    }
}

// MARK: ------ Table delegate
extension KZCheapVC: UITableViewDelegate, UITableViewDataSource {
    
    // MARK: ------ tableview datasource
    func numberOfSections(in tableView: UITableView) -> Int {
        return 10
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell: KZSearchProductCell = tableView.dequeueReusableCell(withIdentifier: "cell") as! KZSearchProductCell
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return UIView()
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let vc = KZProductDetailVC()
        self.navigationController?.pushViewController(vc, animated: true)
    }
}
